package com.tyk.demo.service;

import com.github.pagehelper.PageHelper;
import com.tyk.demo.dto.UserInfo;
import com.tyk.demo.mapper.UserInfoMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class UserInfoServiceImpl implements UserInfoService {
    @Resource
    private UserInfoMapper userinfoMapper;


    @Override
    public boolean insert(UserInfo userinfo) {
        // valid
        if (userinfo == null) {
//            return new ReturnT<String>(ReturnT.FAIL_CODE, "必要参数缺失");
        }
        return userinfoMapper.insert(userinfo)>0;
    }


    @Override
    public boolean delete(String id) {
        int ret = userinfoMapper.delete(id);
        return ret>0;
    }


    @Override
    public boolean update(UserInfo userinfo) {
        int ret = userinfoMapper.update(userinfo);
        return ret>0;
    }


    @Override
    public UserInfo load(String id) {
        return userinfoMapper.load(id);
    }


    @Override
    public Map<String,Object> pageList(int offset, int pagesize) {

        List<UserInfo> pageList = userinfoMapper.pageList(offset, pagesize);
        int totalCount = userinfoMapper.pageListCount(offset, pagesize);

        // result
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("pageList", pageList);
        result.put("totalCount", totalCount);

        return result;
    }

    @Override
    public List<UserInfo> pageList2(UserInfo userInfo) {
        if (userInfo.getPage() != null && userInfo.getRows() != null) {
            PageHelper.startPage(userInfo.getPage(), userInfo.getRows());
        }
        return userinfoMapper.getAll(userInfo);
    }
}
